package action.auth;

import org.apache.struts2.ServletActionContext;

import service.interf.security.IAuthority;

import com.opensymphony.xwork2.ModelDriven;

import dao.pojo.GroupMember;
import action.common.MyBaseAction;

public class UserGroupAction extends MyBaseAction<GroupMember> implements
		ModelDriven<GroupMember>{
	private static final long serialVersionUID = 1L;
	private IAuthority iAuth;
	
	public void setiAuth(IAuthority iAuth) {
		this.iAuth = iAuth;
	}
	
	public IAuthority getiAuth() {
		return iAuth;
	}

	public UserGroupAction() {
		super(GroupMember.class);
	}

	public GroupMember getModel() {
		return model;
	}
	
	/***************************method****************************************/
	/**
	 * 根据用户Id获取用户所在的组
	 */
	public void getUserGroup(){
		if(!validMe("user.userId"))
			return;
		String r = null;
		try{
			r = iAuth.getUserGroups(model.getUser().getUserId());
		}catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		this.servletHandle.writeToClient(ServletActionContext.getResponse(), r);
	}
	
	/**
	 * 更新用户所在的组
	 */
	public void setUserGroup(){
		if(!validMe("user.userId"))
			return;
		String r = null;
		try{
			r = iAuth.updateUserGroups(model.getUser().getUserId(), model.getGroup().getIds());
		}catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		this.servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}
}
